package com.lg.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.product.entity.Brand;
import com.lg.product.entity.Category;
import com.lg.product.entity.Spu;
import com.lg.product.mapper.SpuMapper;
import com.lg.product.service.IBrandService;
import com.lg.product.service.ICategoryService;
import com.lg.product.service.ISpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 服务实现类
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements ISpuService {

    @Autowired
    private IBrandService iBrandService;
    @Autowired
    private ICategoryService iCategoryService;

    @Override
    public IPage<Spu> listSpu(Spu spu, Page page) {
        IPage<Spu> spuIPage = baseMapper.listSpu(spu, page);
        List<Spu> records =  spuIPage.getRecords();
        for (Spu res : records) {
            Long brandId = res.getBrandId();
            //获取商品name
            Brand brand = iBrandService.listId(brandId.intValue());
            res.setBrandName( brand.getName());

            StringBuffer stringBuffer = new StringBuffer();
            Long cid1 = res.getCid1();
            Category category1 = iCategoryService.listId(cid1.intValue());
            stringBuffer.append(category1.getName()+" / ");
            Long cid2 = res.getCid2();
            Category category2 = iCategoryService.listId(cid2.intValue());
            stringBuffer.append(category2.getName()+" / ");
            Long cid3 = res.getCid3();
            Category category3 = iCategoryService.listId(cid3.intValue());
            stringBuffer.append(category3.getName());
            res.setCategoryName(stringBuffer.toString());
        }
        return spuIPage;
    }

    /**
     * 修改 上架 下架
     * @param id
     * @param saleable
     * @return
     */
    public Long updateSaleable(Long id,Integer saleable){
        Long aLong = baseMapper.updateSaleable(id, saleable);

        return aLong;
    }

    @Override
    public Spu listById(Long id) {
        Spu spu = baseMapper.listById(id);
        return spu;
    }
}
